package com.example.controller;

import com.example.service.HazelcastService;
import com.hazelcast.collection.IQueue;
import com.hazelcast.core.EntryAdapter;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.map.IMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class TestController implements InitializingBean, CommandLineRunner {

    public static final String MAP = "MAP222";

    public static final String QUEUE = "QUEUE222";

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private HazelcastService hazelcastService;

    @RequestMapping("put")
    public String put() {
        IMap<String, String> map = hazelcastService.getHazelcastInstance().getMap(MAP);
        String uuid = UUID.randomUUID().toString();
        map.put(uuid, uuid, 10, TimeUnit.SECONDS);
        return uuid;
    }

    @RequestMapping("publishDelay")
    public boolean publishDelay() throws Exception {
        IQueue<String> queue = hazelcastService.getHazelcastInstance().getQueue(QUEUE);
        String uuid = UUID.randomUUID().toString();
        return queue.offer(uuid, 10, TimeUnit.SECONDS);
    }

    @RequestMapping("publish")
    public String publish() throws Exception {
        IQueue<String> queue = hazelcastService.getHazelcastInstance().getQueue(QUEUE);
        String uuid = UUID.randomUUID().toString();
        queue.put(uuid);
        return uuid;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        IMap<String, String> map = hazelcastService.getHazelcastInstance().getMap(MAP);
        map.addEntryListener(new EntryAdapter() {
            @Override
            public void entryAdded(EntryEvent event) {
                logger.warn("entryAdded: " + event.getValue());
            }

            @Override
            public void entryRemoved(EntryEvent event) {
                logger.warn("entryRemoved: " + event.getValue());
            }

            @Override
            public void entryExpired(EntryEvent event) {
                logger.warn("entryExpired: " + event.getOldValue());
            }
        }, true);
    }

    @Override
    public void run(String... args) throws Exception {
        IQueue<String> queue = hazelcastService.getHazelcastInstance().getQueue(QUEUE);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String take = null;
                    try {
                        take = queue.take();
                        logger.warn("监听到消息: " + take);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
    }
}
